<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of message
 *
 * @author Gidaff_01
 */
class Message extends Doggy_Flirt_FrontEnd_Controller {

    //put your code here
    function Message() {
        parent::Doggy_Flirt_FrontEnd_Controller();
        $this->load->model('Message_m');
        $this->load->helper('date');
        $this->_data['erreur'] = "";
    }

    function index() {
        $this->display_view('home/index');
    }

    function send($friend_id) {
        is_user();
        $this->_data['friend_id'] = $friend_id;
        $posted_by = $this->_data['account_id'];
        $this->_data['erreur'] = "";

        $config = array(
            array(
                'field' => 'title',
                'label' => 'Title',
                'rules' => 'required'
            ),
            array(
                'field' => 'content',
                'label' => 'Content',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);
        if ($this->form_validation->run()) {
            $data = array(
                'title' => $this->input->post('title'),
                'content' => $this->input->post('content'),
                'posted_by' => $posted_by,
                'to_friends' => $friend_id,
                'is_read' => 0,
                'posted_date'=>  time()
            );
            $query = $this->Message_m->send_message($data);
            $query_id_max = $this->Message_m->get_reply_id_max();
            $mess = $query_id_max->row();
            $mess_id = $mess->new_id;
            /* 
            $mess = $query_id_max->row();
            $mess_id = $mess->new_id;
            $data = array(
                'content' => $this->input->post('content'),
                'commented_time' => time(),
                'account_id' => $posted_by,
                'message_id' => $mess_id,
                'is_read' => 1,
            );
            $query = $this->Message_m->reply_message($data);
            */
            redirect('message/read_message/'.$mess_id);
        } else {
            $this->display_view('messages/send_message');
        }
    }

    function list_all_messages() {
        is_user();
        $account_id = $this->_data['account_id'];
        $per_page = $this->uri->segment(4);
        if ($per_page == "") {
            $per_page = 7;
        }
        $off_set = $this->uri->segment(5);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/message/list_all_messages/' .$account_id.'/'. $per_page . '/';
        $config['per_page'] = '7';
        $config['uri_segment'] = 5;
        $config['total_rows'] = $this->Message_m->count_all_messages($account_id);
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['messages'] = $this->Message_m->list_all_messages($account_id, $config['per_page'], $off_set);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->Message_m->count_all_messages($account_id);
        $this->_data['page_title'] = "Liste de messages";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->display_view('messages/list_messages');
    }

    function read_message($id) {
        is_user();
        $this->_data['erreur'] = "";
        $message = $this->Message_m->read_message($id);
        $this->_data['read_message'] = $message;

        $data = array(
            'is_read' => 1
        );
        $query = $this->Message_m->is_read($data, $id);
        $this->display_view('messages/read_message');
    }

    function delete_message($id,$account_id) {
        is_user();
        $this->Message_m->delete_message($id,$account_id);
        redirect('message/list_all_messages');
    }
    function delete_reply_message($reply_id,$message_id){
        is_user();
        $account_id = $this->session->userdata('doogy.id');
        $this->Message_m->set_hidden_message($reply_id,$account_id);
        redirect('message/all_reply/'.$message_id);    
    }
    /////
    function search_friend(){
        $key_word = $this->input->post('key_word');
        $account_id = $this->_data['account_id'];
        
        $per_page = 4;
        $config['per_page'] = $per_page;
        $config['uri_segment'] = 5;
        $config['total_rows'] = $this->Message_m->count_search_friend($key_word,$account_id);
        $this->_data['search_result'] = $this->Message_m->search_friend($key_word,$account_id,$per_page,0);
        $config['base_url'] = site_url('message/pagination_search').'/'.$key_word.'/'.$account_id.'/'.$per_page.'/';
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();

        $this->display_view('friends/list_friends');
    }
    
    function pagination_search($key_word,$account_id,$per_page){
        $off_set = $this->uri->segment(7);
        if ($off_set == FALSE) $offset=0;
        $config['per_page'] = $per_page;
        $config['uri_segment'] = 5;
        $config['total_rows'] = $this->Message_m->count_search_friend($key_word,$account_id);
        $this->_data['search_result'] = $this->Message_m->search_friend($key_word,$account_id,$per_page,$off_set);
        $config['base_url'] = site_url('message/pagination_search').'/'.$key_word.'/'.$account_id.'/'.$per_page.'/';
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->display_view('friends/list_friends');
    }
    ///

    function send_wall_message($post_to=null) {
        is_user();
        $posted_by = $this->_data['account_id'];

        $this->_data['erreur'] = "";

        $config = array(
            array(
                'field' => 'wall_message',
                'label' => 'Wall message',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);
        if ($this->form_validation->run()) {
            $data = array(
                'content' => $this->input->post('wall_message'),
                'posted_time' => now(),
                'posted_by' => $posted_by,
                'post_to' =>$post_to
            );
            $query = $this->Message_m->send_wall_message($data);
            redirect('home/show_wall_message');
        } else {
            redirect('home/show_wall_message');
        }
    }
     function send_wall_message_friend($post_to=null) {
        is_user();
        $posted_by = $this->_data['account_id'];

        $this->_data['erreur'] = "";

        $config = array(
            array(
                'field' => 'wall_message',
                'label' => 'Wall message',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);
        if ($this->form_validation->run()) {
            $data = array(
                'content' => $this->input->post('wall_message'),
                'posted_time' => now(),
                'posted_by' => $posted_by,
                'post_to' =>$post_to
            );
            $query = $this->Message_m->send_wall_message($data);
            redirect('home/wall_friend/'.$post_to);
        } else {
            redirect('home/wall_friend/'.$post_to);
        }
    }

    function reply_message($message_id) {
        is_user();
        $account_id = $this->_data['account_id'];
        $this->_data['erreur'] = "";

        $config = array(
            array(
                'field' => 'content',
                'label' => 'Content message',
                'rules' => 'required'
            )
        );
        $this->form_validation->set_rules($config);
        if ($this->form_validation->run()) {
            $data = array(
                'content' => $this->input->post('content'),
                'commented_time' => now(),
                'account_id' => $account_id,
                'message_id' => $message_id,
                'is_read' =>0
            );
            $query = $this->Message_m->reply_message($data);
            $this->db->delete('doggy_messages_delete',array('message_id'=>$message_id));
            redirect('message/all_reply'.'/'.$message_id);
        } else {
            redirect('message/all_reply'.'/'.$message_id);
        }
    }

    function all_reply($message_id) {
        is_user();
        $data = array(
            'is_read' => 1
        );
        $this->Message_m->is_read($data, $message_id);
        $account_id = $this->_data['account_id'];
        $per_page = $this->uri->segment(4);
        if ($per_page == "") {
            $per_page = 7;
        }
        $off_set = $this->uri->segment(5);
        if ($off_set == "") {
            $off_set = 0;
        }
        $config['base_url'] = base_url() . 'index.php/message/all_reply/' .$message_id.'/'. $per_page . '/';
        $config['per_page'] = '7';
        $config['uri_segment'] = 5;
        $config['total_rows'] = $this->Message_m->count_all_reply($message_id);
        if ($off_set > 0 && $off_set == $config['total_rows']) {
            $off_set = $off_set - $per_page;
        }
        $this->_data['reply_message'] = $this->Message_m->all_reply($message_id, $config['per_page'], $off_set);
        $this->_data['message_details'] = $this->Message_m->get_message_details($message_id);
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $this->Message_m->count_all_reply($message_id);
        $this->_data['page_title'] = "Liste de messages";
        $this->_data['per_page'] = $per_page;
        $this->_data['off_set'] = $off_set;
        $this->_data['message_to_list'] = $message_id;
        $this->Message_m->set_read_reply($message_id);
        $this->display_view('messages/list_reply_message');
    }

}

?>
